package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.OrderModel;
import com.apps.ftapp.model.Uaddress;
import com.apps.ftapp.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by Lly on 2017/10/12.
 */

public class UserAddressAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<Uaddress.AddressBean> dataList;
    private LayoutInflater layoutInflater;


    private MyClickListener mListener;

    @Override
    public void onClick(View v) {

        mListener.clickListener(v);

    }


    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener {
        void clickListener(View v);
    }


    public UserAddressAdapter(Context context, List dataList, MyClickListener listener) {
        this.context = context;
        this.dataList = dataList;
        this.mListener = listener;
        layoutInflater = LayoutInflater.from(this.context);

    }


    public void addRes(List<Uaddress.AddressBean> data) {
        if (data != null) {
            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Uaddress.AddressBean bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.activity_user_address_list_listview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.addressName.setText(bean.getUser_name());
        viewHolder.addressMobile.setText(bean.getMobile());
        viewHolder.addressDesc.setText(bean.getAddress());

        viewHolder.address_top.setTag(i);
        viewHolder.address_top.setOnClickListener(this);

        viewHolder.addressEdit.setTag(i);
        viewHolder.addressEdit.setOnClickListener(this);

        viewHolder.addressDel.setTag(i);
        viewHolder.addressDel.setOnClickListener(this);


        return view;
    }


    class ViewHolder {
        TextView addressName;
        TextView addressMobile;
        TextView addressDesc;
        TextView addressEdit;
        TextView addressDel;
        RelativeLayout address_top;

        public ViewHolder(View view) {
            addressName = view.findViewById(R.id.address_name);
            addressMobile = view.findViewById(R.id.address_mobile);
            addressDesc = view.findViewById(R.id.address_address);
            addressEdit = view.findViewById(R.id.address_edit);
            addressDel = view.findViewById(R.id.address_del);
            address_top = view.findViewById(R.id.address_top);

        }
    }
}
